<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:32
 */

namespace app\api\controller;


use app\common\model\ChatMoneyLog;
use app\common\model\MallConfig;
use app\model\CardsModel;
use app\model\ChatmsgModel;
use app\service\LogService;
use app\service\MoneylogService;
use app\service\OrderService;
use app\service\UserService;
use app\service\WechatService;
use app\service\WithdrawalService;
use app\common\model\User as UserModel;
use app\common\model\Withdrawal as WithdrawalModel;
use app\common\model\Order as OrderModel;
use app\common\model\MallConfig as MallConfigModel;
use app\common\model\Grade as GradeModel;
use app\common\model\UserWechat as UserWechatModel;
use app\common\model\Daysign;
use app\common\model\Qiniu;
use app\common\model\Card;
use app\service\MsgService;
use app\service\PayService;


use think\Cache;
use think\Db;
use think\Exception;

class User extends Base
{
    public function __construct()
    {
        parent::__construct();
    }



    //个人中心首页
    public function index(){

        $user_info=UserService::S()->findUser(array('id'=>$this->uid));
        $user_info['grade_name']=UserService::S()->backAllGradeArray()[$user_info['grade_id']];
        $order_where['uid']=$this->uid;
        $order_info['all_order']=OrderService::S()->countOrder($order_where);

        //待支付订单
        $order_where['checked']=0;
        $order_info['wait_pay_order']=OrderService::S()->countOrder($order_where);

        //待发货订单
        $order_where['checked']=1;
        $order_info['wait_deliver_order']=OrderService::S()->countOrder($order_where);

        //已完成订单
        $order_where['checked']=2;
        $order_info['over_order']=OrderService::S()->countOrder($order_where);

        $user_info['order']=$order_info;

        //我的人气
        $xiajiList=UserService::S()->backAllXiaji($this->uid);
        $my_team['count_user']=count($xiajiList);
        $my_team['count_user_order']=OrderService::S()->countOrder(array('uid'=>array('in',$xiajiList)));
        $user_info['my_team']=$my_team;

        //直推总业绩
        $where = "id={$this->uid} or refer_id={$this->uid}";
        $uids = UserModel::where($where)->column('id');
        $orderwhere = [
            'uid' => [
                'in',$uids
            ],
            'checked' => [
                'in',[1,2,5]
            ],
            'shop_type' => [
                'in',[1,4]
            ]
        ];
        $all = OrderModel::where($orderwhere)->sum('goods_money');
        $user_info['xiaji_yeji'] = $all;

        //是否是聊天室管理员
        $config = MallConfigModel::getConfig($this->mall_id);
        //$managerArr = explode(',',$config['chat_manager']);
        if(UserModel::isManager($this->uid)) {
            $user_info['chat_manager'] = 1;
        }else{
            $user_info['chat_manager'] = 0;
        }
        if(UserModel::isLiveManager($this->uid)) {
            $user_info['live_manager'] = 1;
        }else{
            $user_info['live_manager'] = 0;
        }
        //是否关注
        $user_info['subscribe'] = UserWechatModel::where('uid',$user_info['id'])->value('subscribe');
        $user_info['referInfo'] = UserModel::getOneById($user_info['refer_id'],'id,nickname,realname,mobile');
        
        $user_info['cash'] = round($user_info['cash']*config('dikou_rate'),2);
        //生态商城用户编号
        $user_info['pid'] = config('site_no').(100+$this->mall_id).$user_info['id'];

        //今日是否签到
        $signLog = Daysign::getOne([
            'uid' => $this->uid,
            'create_date' => date('Y-m-d')
        ]);
        $user_info['daysign'] = $signLog?1:0;
        $user_order_Where['checked']=array('in',array(1,2,5));
        $user_order_Where['uid']=$this->uid;

        $user_order=OrderService::S()->countOrder($user_order_Where);

        $user_info['is_vip']=$user_order>0?1:0;
        if($this->user['grade_id']>1){
            $user_info['is_vip']=1;
        }

        $daysign = $this->is_series_daysign($this->uid);
        $min_value = config('site_daysign_min');
        $max_value = config('site_daysign_max');
        $day = 0;
        if ($daysign) {
            $min_value = config('site_daysign_serial') * $daysign['day'] + $min_value;
            $max_value = config('site_daysign_serial') * $daysign['day'] + $max_value;
            $day = $daysign['day'];
        }
        $min_value = $min_value > config('site_daysign_serial_max') ? config('site_daysign_serial_max') : $min_value;
        $max_value = $max_value > config('site_daysign_serial_max') ? config('site_daysign_serial_max') : $max_value;

        $user_info['daysign_day'] = $day;
        $user_info['daysign_min'] = $min_value;
        $user_info['daysign_max'] = $max_value;
        //名片分享图片
        $pic = CardsModel::get(['user_id'=>$this->uid]);
        if($pic){
            $pic = $pic->toArray();
            $user_info['share_pic'] = $pic['share_pic']?tomedia($pic['share_pic']):'';
        }else{
            $user_info['share_pic'] = '';
        }
        //是否为客服
        $mall_config = MallConfig::getOne(['mall_id' => $this->mall_id, 'name' => 'chat_kefu_manager']);
        if ($mall_config) {
            $mall_config = explode(',', $mall_config['value']);
            if (in_array($this->uid, $mall_config)) {
                $user_info['is_kefu'] = true;
            } else {
                $user_info['is_kefu'] = false;
            }
            $user_info['kid'] = $mall_config[array_rand($mall_config)];
        } else {
            $user_info['is_kefu'] = false;
        }
        $this->ajax(200,'success',$user_info);
    }

    //查询用户可使用金额
    public function find_user_money(){
        $user_info=UserService::S()->findUser(array('id'=>$this->uid),'money,money');
        if($user_info){
            $this->ajax(200,'success',$user_info);
        }else{
            $this->ajax(202,'用户不存在');
        }
    }

   //余额明细
    public function money_log(){
        $input = input();
        $page = max($input['page'],1);
        $page_num=20;
        $where=array('uid'=>$this->uid);
        if($input['type']) {
            $where['type'] = $input['type'];
        }else{
            $where['type'] = 1;
        }
        $data=MoneylogService::S()->selectList($where,$page,$page_num);
        $count=MoneylogService::S()->countList($where);
        if(config('redbag_type')==2 && $input['type']==3) {
            foreach($data as $k=>$v) {
                $data[$k]['money'] = (string)round(config('dikou_rate')*$data[$k]['money'],2);
            }
        }
        $backData['data']=$data;
        $backData['code']=200;
        $backData['msg']='success';
        $backData['page_count']=ceil($count/$page_num);
        $this->ajax(200,'success',$data);
    }

    //用户提现
    public function withdrawal(){
        try{
            $save['bank_name']=input('post.bank_name');
            if(!self::bankIsOpen($save['bank_name'])){
                $this->ajax(202,'该银行未开启提现');
            }
            $save['bank_card_name']=input('post.bank_card_name');
            $save['bank_card']=input('post.bank_card');
            $save['remark']=input('post.remark');
            $save['uid']=$this->uid;
            $save['kaihuhang']=input('post.kaihuhang');
            $save['money']=input('post.money');
            $save['tax_money'] = round(input('post.money')*config('finance_tax_bili'),2);
            $save['atm_money'] = round($save['money']-$save['tax_money'],2);
            $save['create_time']=time();
            if($save['bank_name']!='微信钱包') {
                if(!$save['bank_card_name'] || !$save['bank_card'] || !$save['kaihuhang']) {
                    $this->ajax(202,'请填写完整');
                }
            }else{
                if(!$save['bank_card_name']) {
                    $this->ajax(202,'请填写完整');
                }
            }

            if($save['money']<=0) {
                $this->ajax(202,'提现金额错误');
            }
            if($save['money']>$this->member['money']){  //提现余额大于用户余额
                $this->ajax(202,'提现金额超出');
            }
            if(config('finance_tax_per')>0) {
                if($save['money']%config('finance_tax_per')!=0 || $save['money']<config('finance_tax_per')) {
                    $this->ajax(202,'提现金额必须是'.config('finance_tax_per').'的倍数');
                }
            }
            $re=WithdrawalService::S()->addList($save);
            if($re){
                //写入提现日志-=
                /* $moneyLog['uid']=$this->uid;
                $moneyLog['order_id']=0;
                $moneyLog['mtype']=2;//佣金提现
                $moneyLog['log_type']=1;//支出
                $moneyLog['money']=$save['money'];
                $moneyLog['content']='【提现佣金】';
                $moneyResult=MoneylogService::S()->add($moneyLog);
                if($moneyResult){ //扣用户佣金
                    UserService::S()->decFiled($this->uid,$save['money'],'money');
                } */
                UserModel::changeMoney($this->uid,1,-$save['money'],'余额提现');

                //小金额自动打款
                if($save['band_name']=='微信钱包' && $save['money']<=config('finance_atm_auto')) {
                    $log = WithdrawalModel::getOneById($re);
                    $userInfo = UserModel::getOneById($log['uid']);
                    $result = PayService::S(['mall_id'=>$userInfo['mall_id']])->qiyePay($log);
                    if($result===true) {
                        $save = [
                            'status' => 2,
                            'update_time' => time()
                        ];
                        $res = WithdrawalModel::where('id',$log['id'])->update($save);
                    }else{
                    }
                }


                $this->ajax(200,'申请提现成功,请耐心等待');
            }else{
                $this->ajax(202,'申请提现失败,请重试');
            }
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            $this->ajax(202,'Exception',$ex->getMessage());
        }
    }

    //用户绑定银行卡
    public function bind_card(){
        $save['bank_name']=input('post.bank_name');

        if(!self::bankIsOpen($save['bank_name'])){
            $this->ajax(202,'该银行未开启');
        }
        $save['bank_card_name']=input('post.name');
        $save['bank_card']=input('post.bank_card');
        $sure_bank_card=input('post.sure_bank_card');
        if($save['bank_card']!=$sure_bank_card){
            $this->ajax(202,'两次银行卡号不一致');
        }
        //$save['remark']=input('post.remark');
        $save['kaihuhang']=input('post.kaihuhang');

        $re=UserService::S()->saveUserInfo(array('id'=>$this->uid),$save);//WithdrawalService::S()->addList($save);
        if($re===false){
            $this->ajax(202,'银行卡绑定失败,请重试');
        }else{
            $this->ajax(200,'银行卡绑定成功,请耐心等待');
        }
    }

    /**
     * 查询银行是否开启
     * @param $bank_name
     * @return bool
     */
    public function bankIsOpen($bank_name){
        try{
            $bank_name=trim($bank_name);
            $re=Db::name('bankcom')->where(array('status'=>1,'name'=>$bank_name))->find();
            if($re){
                return true;
            }else{
                return false;
            }
        }catch (Exception $ex){
            return false;
        }
    }

    //提现记录
    public function withdrawal_list(){
        $page=input('post.page');
        if($page<1){
            $page=1;
        }
        $page_num=20;
        $list=WithdrawalService::S()->selectListByUid($this->uid,$page,$page_num);
        $data['code']=200;
        $data['msg']='success';
        $data['data']=$list;
        $data['page_count']=ceil(WithdrawalService::S()->countWithdrawal(array('uid'=>$this->uid))/$page_num);
        echo json_encode($data);exit();/*
        $this->ajax(200,'success',$list);*/
    }


    //查询可提现银行列表
    public function bank_list(){
        try{
            $field='id,name as bank_name';
            $result=Db::name('bankcom')->field($field)->where(array('status'=>1))->select()->toArray();
            $this->ajax(200,'success',$result);
        }catch (Exception $ex){
            echo $ex->getMessage();
        }
    }

    /*
     * 我的二维码
     */
    public function qrcode_index(){


        $userinfo = $this->member;
        //获取用户信息
        $wxuser = UserService::S()->getWechatUserInfo($this->uid);;//$this->wechat->getUserInfo($userinfo['openid']);
        if(!empty($wxuser)){
            $userinfo['nickname'] = $wxuser['nickname'] ? $wxuser['nickname'] : $userinfo['nickname'];
            $userinfo['headimgurl'] = $wxuser['headimgurl'] ? $wxuser['headimgurl'] : $userinfo['headimgurl'];
        }

        $share_pic = $this->makeshare($userinfo,1);//生成分享二维码
        if($share_pic){
            $this->assign("share_pic",$share_pic);
        }
    }

    /*
    * 生成推广二维码图片
    */
    public function makeshare($userinfo,$refresh=0){

        $uid = $userinfo['uid'];
        $headimgurl = $userinfo['headimgurl'];


        $nickname = $userinfo['realname'] ? $userinfo['realname'] : $userinfo['nickname'];
        if(!$nickname){
            $nickname = config("SITE_NAME");
        }

        if(!$uid){
            $uid = $this->uid;
        }
        $file = ROOT_PATH.'/upload/share/qr'.$uid.".jpg";//存储的文件名
        $pic_url = getDomain()."/upload/share/".$uid.".jpg";
        if(file_exists($file) && !$refresh){
            //如果文件存在 并且不需要更新
            return $pic_url;
        }

        //获取二维码图片
        $qrcode_pic = $this->qrcodeurl($uid,$refresh);
        if(!$qrcode_pic){
            return false;//生成二维码失败
        }
        $grade = $userinfo['grade_id'] ? $userinfo['grade_id'] : 1;
        //$background_url = C("UPYUN_DOMAIN").C("QRCODE_GRADE".$grade);
        $background_url = config("QRCODE_GRADE".$grade);


        //$background_url = "http://qiniu.vlu2.cn/_upload_other_2018-09_07_806511536328822.jpg";

        $arr = array(
            array(
                'text' => qiniubase64url($userinfo['nickname']),
                'dx' => 0,
                'dy' => 180,
                'gravity' => 'North',
                'fontsize' => '600',
                'fill' => qiniubase64url('#ffffff'),
            ),
            array(
                'text' => qiniubase64url('ID: '.$uid),
                'dx' => 0,
                'dy' => 240,
                'gravity' => 'North',
                'fontsize' => '400',
                'fill' => qiniubase64url('#ffffff'),
            ),
            array(
                'image' => qiniubase64url($qrcode_pic),
                'dx' => 0,
                'dy' => -5,
                'gravity' => 'Center',
                'ws' => 0.43
            )
        );
        if(!empty($arr)) {
            $background_url .= "?watermark/3";
        }
        foreach($arr as $v) {
            if($v['text']) {
                $background_url .= "/text/{$v['text']}";
                if($v['fontsize']) {
                    $background_url .= "/fontsize/{$v['fontsize']}";
                }
                if($v['fill']) {
                    $background_url .= "/fill/{$v['fill']}";
                }
            }else{
                $background_url .= "/image/{$v['image']}";
            }
            $background_url .= "/dx/{$v['dx']}";
            $background_url .= "/dy/{$v['dy']}";
            $background_url .= "/gravity/{$v['gravity']}";
            if($v['ws']) {
                $background_url .= "/ws/{$v['ws']}";
            }

        }
        return $background_url;
    }


    //获取二维码地址
    public function qrcodeurl($uid,$refresh=0){
        if(!$uid){
            $uid = $this->uid;
        }
        /* $text = get_domain()."/mobile/login/register?refer=".$uid;//内容
        $pic_url = "http://qr.liantu.com/api.php?text=".$text;//图片网址
        return $pic_url; */

        $pic_url = get_domain()."/upload/qrcode/qr".$uid.".png";//图片网址
        $outfile = ROOT."/upload/qrcode/qr".$uid.".png";
        if(!file_exists($outfile) || $refresh){

            //qrcode($text, $outfile, $level = 1, $size = 8, $margin = 1,$saveandprint=false);

            $scene_id = "FENXIAO_UID_".$uid;
            $ticket_arr = $this->wechat->getQRCode($scene_id,2);//获取二维码ticket
            if(isset($ticket_arr['url'])){
                //getQRUrl($ticket)
                $text = $ticket_arr['url'];
                //return $pic_url = "http://qr.liantu.com/api.php?text=".$text;
                qrcode($text, $outfile, $level = 1, $size = 8, $margin = 1,$saveandprint=false);
            }

            if(!file_exists($outfile)){
                //不存在,生成失败
                return false;
            }
        }
        return $pic_url;
    }

    /* 绑定手机 */
    public function bindMobile () {
        $input = input();
        if(!$input['mobile']) {
            $this->ajax(202,'请填写手机号');
        }
        if(!$input['code']) {
            $this->ajax(202,'请填写验证码');
        }
        if(!$input['realname']) {
            $this->ajax(202,'请填写姓名');
        }
        $code = cache("alisms-code-{$this->uid}-{$input['mobile']}");
        if(!$code || $code!=$input['code']) {
            $this->ajax(202,'验证码错误');
        }

        $user_where['mobile']=$input['mobile'];
        $user_where['mall_id']=$this->mall_id;
        $user=\app\common\model\User::getOne($user_where,'id');
        if($user && $user['id']!=$this->uid){
            $this->ajax(202,'该手机号码在该商城已被注册请换一个');
        }

        $res= UserModel::where('id',$this->uid)->update([
            'mobile' => $input['mobile'],
            'realname' => $input['realname']
        ]);
        if(!$res) {
            $this->ajax(202,'无任何修改');
        }else{

            if(empty( $this->member['mobile'])){ //手机号码是空的
                $money=10;
                $log = [
                    'uid' => $this->uid,
                    'money_id' => $input['id'],
                    'money' => 10,//$hongbao['arr'][$count]
                ];
                ChatMoneyLog::create($log);
                //type 1:现金  2:积分  3:抵扣金
                $type = config('redbag_type')==1? 1: 3;
                UserModel::changeMoney($this->uid,$type,$log['money'],$content='完善信息赠送抵扣金');
                /*ChatmsgModel::create([
                    'uid' => $this->uid,
                    'content' => json_encode([
                        'type' => 'message',
                        'uid' => $this->uid,
                        'content' => [
                            'type' => 'money_get',
                            'money' => $log['money'],
                            'nickname' => $this->member['nickname'],
                            'uid' => $this->uid
                        ]
                    ]),
                    'status' => 0,
                    'group_id' => UserModel::getGroupId($this->uid),
                    'type' => 'money_get'
                ]);
                */

                $money=$money*10;
                WechatService::S([
                    'mall_id' => $this->member['mall_id']
                ])->send(
                    1,
                    UserModel::uid2openid($this->uid),
                    "【完善信息】获得赠送抵扣金:{$money}"
                );

                /*UserModel::changeMoney($this->uid,$type,$money,date('Y-m-d').'完善信息赠送积分');
                $userInfo = UserModel::getOneById($this->uid);
                //发消息 TODO
                WechatService::S([
                    'mall_id' => $this->member['mall_id']
                ])->send(
                    1,
                    UserModel::uid2openid($this->uid),
                    "【完善信息】赠送积分+{$money},您累计积分{$userInfo['score']}"
                );
                $card = Card::getOne(1);
                $url = Qiniu::buildWaterMark($card['bg_picture'],json_decode($card['content'],true),$this->uid);
                $path = Qiniu::downloadFile($url,'.png');

                $media = WechatService::S()->uploadImage($path);
                $media_id = $media['media_id'];
                WechatService::S()->send(3,UserModel::uid2openid($this->uid),$media_id);*/
                // $this->ajax(200,'已签到');
            }else{
                $this->ajax(202,'请重试');
            }

        }
        $this->ajax(200,'修改成功');
    }

    /* 绑定推荐人 */
    public function bindRefer () {
        $input = input();
        $userInfo = UserModel::getOne(['id'=>$this->uid]);
        if($userInfo['refer_id']) {
            $this->ajax(200,'error');
        }
        $referInfo = UserModel::getOne(['id'=>$input['refer_id']]);
        $refer_id = $input['refer_id'];
        if(!UserModel::checkRefer($this->uid,$refer_id)) {
            $this->ajax(200,'error');
        }
        $res = UserModel::where(['id'=>$this->uid])->update([
            'refer_id' => $refer_id
        ]);
        if($res) {
            //TODO 消息通知
            MsgService::S(['mall_id'=>$referInfo['mall_id']])->sendTemp('NEW_USER', $referInfo['mall_id'],$referInfo['id'],[
                'first' => '有新用户加入',
                'nickname' => $userInfo['nickname'],
                'remark' => ''
            ]);
            $this->ajax(200,'绑定成功');
        }
        $this->ajax(200,'绑定失败请重试');
    }

    /* 获取下级用户列表 */
    public function xiaji () {
        $input = input();
        $where= [
            'refer_id' => $this->uid
        ];
        if($input['name']) {
            $where['nickname|realname'] = [
                'like',"%{$input['name']}%"
            ];
        }
        if($input['grade_id']) {
            $where['grade_id'] = $input['grade_id'];
        }
        $page = max($input['page'],1);
        $size = 20;
        $list = UserModel::getPage($where,$page,$size,'id desc','id,nickname,headimgurl,mobile,grade_id,realname');
        if(!$list) {
            $this->ajax(200,'success',[]);
        }
        foreach($list as $k=>$v) {
            $list[$k]['grade_name'] = GradeModel::id2name($v['grade_id']);
        }
        $this->ajax(200,'success',$list);
    }

    /* 直推总业绩统计 */
    public function xiajiYeji () {
        $where = "id={$this->uid} or refer_id={$this->uid}";
        $uids = UserModel::where($where)->column('id');
        $orderwhere = [
            'uid' => [
                'in',$uids
            ],
            'checked' => [
                'in',[1,2,5]
            ],
            'shop_type' => [
                'in',[1,4]
            ]
        ];
        $all = OrderModel::where($orderwhere)->sum('goods_money');
        $this->ajax(200,'success',$all);
    }

    /* 获取等级列表 */
    public function gradeList () {
        $list = GradeModel::getAll(1,'id asc');
        $this->ajax(200,'success',$list);
    }


    /* 每日打卡 */
    public function daysign () {
        $where = [
            'uid' => $this->uid,
            'create_date' => date('Y-m-d')
        ];
        $log = Daysign::getOne($where);
        if($log) {
            $this->ajax(202,'请勿重复礼佛');
        }
        $daysign = $this->is_series_daysign();
        $money =config('site_daysign_serial')*$daysign['day'] + round(rand(config('site_daysign_min')*100,config('site_daysign_max')*100)/100,2);
        $money = $money>config('site_daysign_serial_max')?config('site_daysign_serial_max'):$money;

        $type = 2;
        $log = [
            'uid' => $this->uid,
            'create_date' => date('Y-m-d'),
            'money' => $money,
            'money_type' => $type
        ];
        $res = Daysign::create($log);
        if($res) {
            UserModel::changeMoney($this->uid,$type,$money,date('Y-m-d').'礼佛积分');
            $userInfo = UserModel::getOneById($this->uid);
            //发消息 TODO
            WechatService::S([
                'mall_id' => $this->userInfo['mall_id']
            ])->send(
                1,
                UserModel::uid2openid($this->uid),
                "【礼佛成功】赠送积分+{$money},您累计积分{$userInfo['score']}"
            );

            $cardModel = new Card();
            $card = $cardModel->getRand();
            $url = Qiniu::buildWaterMark($card['bg_picture'],json_decode($card['content'],true),$this->uid);
            $path = Qiniu::downloadFile($url,'.png');

            $media = WechatService::S()->uploadImage($path);
            $media_id = $media['media_id'];
            WechatService::S()->send(3,UserModel::uid2openid($this->uid),$media_id);
            $this->ajax(200,'已礼佛',$money);
        }else{
            $this->ajax(202,'请重试');
        }
    }

    /* 签到日历 */
    public function signCalendar () {
        $beginDate=date('Y-m-01', strtotime(date("Y-m-d")));
        $endDate = date('Y-m-d', strtotime("$beginDate +1 month -1 day"));
        $where = [
            'create_date' => [
                'between' , [$beginDate,$endDate]
            ],
            'uid' => $this->uid
        ];
        $list = Daysign::getAll($where,'id asc','id,uid,money,create_date,create_time');
        $newList = [];
        if($list) {
            foreach($list as $v) {
                $newList[$v['create_date']] = $v;
            }
        }
        $dayNum = explode('-',$endDate)[2];
        $result = [];
        for($i=0;$i<$dayNum;$i++) {
            $d = date('Y-m-d', strtotime("{$beginDate} +{$i} day"));
            $result[$d] = $newList[$d]?$newList[$d]:0;
        }
        $this->ajax(200,'已签到',$result);
    }

    /**
     * 判断连签
     * @return array|bool
     */
    private function is_series_daysign($uid = 0)
    {
        if (!$uid)
            $uid = $this->uid;
        $log = db('daysign')->where('uid', $uid)->order('create_date desc')->select();
        if ($log) {
            $log = $log->toArray();
            $date = date('Y-m-d');
            if($log[0]['create_date'] == $date){
                unset($log[0]);
                $day = 1;
            }else{
                $day = 0;
            }
            foreach ($log as $key => $value) {
                $date = date('Y-m-d', strtotime($date) - 24 * 60 * 60);
                if ($value['create_date'] == $date) {
                    $day++;
                } else {
                    break;
                }
            }
            return [
                'day' => $day,
            ];
        }
        return false;

    }

    /* 用户信息 */
    public function infoByUniid () {
        $input = input();
        $info = UserModel::getOne([
            'uniid' => $input['uniid']
        ],'nickname,headimgurl,realname,mobile,uniid,id');
        $this->ajax(200,'success',$info);
    }

    /**
     * 更新uniid
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function updateUniid(){
        $data = Db::table('zkhb_user')->where('uniid','null')->select()->toArray();
        foreach ($data as $value){
            $save_data['uniid'] = md5($value['openid'].time().uniqid());
            Db::table('zkhb_user')->where('id',$value['id'])->update($save_data);
        }
        _pr(1);
    }

}